// recorder-worklet.js
const CHANNELS = 1;
const RATE = 16000;
const SAMPLESIZE = 16;
const CHUNK = 512;


class RecorderWorkletProcessor extends AudioWorkletProcessor {
    process(inputs) {
        const inputChannels = inputs[0][0];
        const audio_buffer = new Int16Array(inputChannels.length);
            inputChannels.forEach((sample, index) => {
                const s = Math.max(-1, Math.min(1, sample));
                audio_buffer[index] = s < 0 ? s * 0x8000 : s * 0x7FFF;
            });
            // 发送数据到主线程
            this.port.postMessage(audio_buffer, [audio_buffer.buffer]);
            // console.log(audio_buffer);
        return true;
    }
}

registerProcessor('recorder-worklet', RecorderWorkletProcessor);  
